﻿// <copyright file="HandleComboBox.cs" company="$registeredorganization$">
// Copyright (c) $year$ $registeredorganization$ All Rights Reserved
// </copyright>
// <author>$registeredorganization$</author>
// <email>mischero@microsoft.com</email>
// <date>$year$-01-01</date>
// <summary>Web analytics service behavior class definition</summary>

namespace Microsoft.WebAnalytics.Behaviors.Handlers
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows.Controls;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Track a combo box
    /// </summary>
    [Tracker(typeof(ComboBox))]
    public class HandleComboBox : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track the media element events
        /// </summary>
        /// <param name="associatedObject">the associated object</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        /// <exception cref="ArgumentNullException">if analyticsEvent is null.</exception>
        public void Track(
            System.Windows.FrameworkElement associatedObject, 
            AnalyticsEvent analyticsEvent, 
            System.EventArgs eventArgs)
        {
            if (analyticsEvent == null)
            {
                throw new ArgumentNullException("analyticsEvent");
            }

            if (analyticsEvent.Name == "SelectionChanged")
            {
                var args = eventArgs as SelectionChangedEventArgs;

                var newItems = (from newItem in args.AddedItems.Cast<object>()
                               select newItem.ToString()).ToArray();

                var items = string.Join("|", newItems);

                analyticsEvent.ActionValue = items;
            }

            this.Log(analyticsEvent);
        }

        #endregion
    }
}
